<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\User;
use App\Http\Requests\storeUserPostRequest;
use DB;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = DB::table('users')
            //$query 代表查询对象
            ->where(function($query)use($request){
            //做逻辑判断  如果进入页面存在了搜索的条件,则执行
            if($request->input('keywords')){
                //封装查询条件 $query代表当前生成的查询对象 funtion处的query意思是需要写入一个参数 传入方法中
                $query->where('name','like','%'.$request->input('keywords').'%')//按照name搜索
                ->orwhere('email','like','%'.$request->input('keywords').'%')
                ->orwhere('phone','like','%'.$request->input('keywords').'%');
            }
            })
            ->paginate($request->input('num',5));

     return view('admin.showUserList',['users'=>$data,'request'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //加载添加用户视图
        return view('admin.userAdd');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(storeUserPostRequest $request)
    {
    
        //执行添加操作
        $user = new user();
        $user->name = $request->name;
        $user->password = Hash::make($request->password);
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->nickName = $request->nickName;
        $user->save();
        return redirect('/user');
    }

    /**
     * 由于自动跳入show方法
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //用户删除
        $user = User::find($id)->delete(); 
        return redirect('/user');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        return view('admin.userEdit')->with('user',$user);
    }

    /**
     * 执行修改
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(storeUserPostRequest $request, $id)
    {

        //执行添加操作
        $user = new user();
        $user->name = $request->name;
        $user->password = Hash::make($request->password);
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->nickName = $request->nickName;
        $user->save();
        return redirect('/user');
        $user = User::where('id','=',$id)->update($request->except('_token','_method'));
        return redirect('/user');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
    }
}
